package com.zhiyou.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou.model.Like;
import com.zhiyou.model.Share;
import com.zhiyou.model.ShareImg;
import com.zhiyou.service.ShareService;
import com.zhiyou.utils.JsonResult;

@RestController
@RequestMapping("share/")
public class ShareController {

	@Autowired
	ShareService shareService;

	/**
	 * 添加一条说话
	 * @param share	
	 * @param files
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("add")
	public JsonResult add(Share share, MultipartFile[] files, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			shareService.addShare(share,files);
			return new JsonResult("插入成功");
		} catch (Exception e) {
			return new JsonResult(500, "插入失败");
		}
	}
	
	/**
	 * 点赞
	 * @param like
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("addLike")
	public JsonResult addLike(Like like, HttpServletRequest request, HttpServletResponse response) {
		try {
			shareService.addLike(like);
			// 获取说说id
			Integer share_id = like.getShare_id();
			// 查询到说说
			Share share = shareService.selectById(share_id);
			// 更改说的点赞数量
			share.setLike_num(share.getLike_num()+ 1);
			shareService.updaet(share);
			return new JsonResult("点赞成功");
		} catch (Exception e) {
			return new JsonResult(500, "点赞失败");
		}
	}

	/**
	 * 根据登录老师id查询老师的朋友圈
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("list/{id}")
	public JsonResult showTeacher(@PathVariable("id") int id, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			List<Share> list = shareService.selectAll(id);
			// 更改说的点赞数量
			return new JsonResult(list, "老师查询成功");
		} catch (Exception e) {
			return new JsonResult(500, "老师查询失败");
		}
	}

	/**
	 * 根据登录家长id查询老师的朋友圈
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("listP/{id}")
	public JsonResult show(@PathVariable("id") int id, HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Share> list = shareService.selectAllByPar_id(id);
			// 更改说的点赞数量
			return new JsonResult(list, "家长查询成功");
		} catch (Exception e) {
			return new JsonResult(500, "家长查询失败");
		}
	}

	/**
	 * 根据说说id查看说说详细信息
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@GetMapping("{id}")
	public JsonResult showShare(@PathVariable("id") int id, HttpServletRequest request, HttpServletResponse response) {
		try {
			Share share = shareService.selectById(id);
			// 更改说的点赞数量
			return new JsonResult(share, "详细查询成功");
		} catch (Exception e) {
			return new JsonResult(500, "详细查询失败");
		}
	}
	/**
	 * 删除说说
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@DeleteMapping("delete/{id}")
	public JsonResult delete(@PathVariable("id") int id, HttpServletRequest request, HttpServletResponse response) {
		try {
			shareService.deleteShare(id);
			// 更改说的点赞数量
			return new JsonResult("删除成功");
		} catch (Exception e) {
			return new JsonResult(500, "删除失败");
		}
	}
	/**
	 * 根据传入的id批量删除说说
	 * @param ids
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping("delete")
	public JsonResult deleteAll(int[] ids, HttpServletRequest request, HttpServletResponse response) {
		try {
			shareService.deleteShareAll(ids);
			// 更改说的点赞数量
			return new JsonResult("批量删除成功");
		} catch (Exception e) {
			return new JsonResult(500, "批量删除失败");
		}
	}
}
